import ssl

import aiohttp
import asyncio
import json


class SyncToE16Device:
    async def sync_to_device(self, param_entity):

        # ===============================================================
        type1 = param_entity['type1']
        type2 = param_entity['type2']
        type3 = param_entity['type3']
        displayName1 = param_entity['displayName1']
        displayName2 = param_entity['displayName2']
        displayName3 = param_entity['displayName3']
        # ==============================================================================
        homePageLayoutScheduleEntities = [
            {
                "id": "1955816962136375296",
                "name": "Name",
                "enable": 1,
                "daySecondsUtc": 30016,
                "repeatWeekUtc": [
                    "1"
                ],
                "scheduleId1": 1,
                "displayName1": displayName1,
                "type1": type1,
                "value1": "",
                "scheduleId2": 2,
                "displayName2": displayName2,
                "type2": type2,
                "value2": "",
                "scheduleId3": 3,
                "displayName3": displayName3,
                "type3": type3,
                "value3": "",
                "scheduleId4": 4,
                "displayName4": "",
                "value4": "",
                "scheduleId5": 5,
                "displayName5": "",
                "value5": "",
                "scheduleId6": 6,
                "displayName6": "",
                "value6": "",
                "deviceId": "0C110524C835",
                "homepageLayoutTypeOptions1": [
                    {
                        "id": 1,
                        "name": "Temp Key",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 2,
                        "name": "PIN",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 3,
                        "name": "Call",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 6,
                        "name": "Speed Dial",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 7,
                        "name": "NULL",
                        "check": True,
                        "selected": False
                    }
                ],
                "homepageLayoutTypeOptions2": [
                    {
                        "id": 1,
                        "name": "Temp Key",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 2,
                        "name": "PIN",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 3,
                        "name": "Call",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 6,
                        "name": "Speed Dial",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 7,
                        "name": "NULL",
                        "check": True,
                        "selected": False
                    }
                ],
                "homepageLayoutTypeOptions3": [
                    {
                        "id": 1,
                        "name": "Temp Key",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 2,
                        "name": "PIN",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 3,
                        "name": "Call",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 6,
                        "name": "Speed Dial",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 7,
                        "name": "NULL",
                        "check": True,
                        "selected": False
                    }
                ],
                "homepageLayoutTypeOptions4": [
                    {
                        "id": 1,
                        "name": "Temp Key",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 2,
                        "name": "PIN",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 3,
                        "name": "Call",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 6,
                        "name": "Speed Dial",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 7,
                        "name": "NULL",
                        "check": True,
                        "selected": False
                    }
                ],
                "homepageLayoutTypeOptions5": [
                    {
                        "id": 1,
                        "name": "Temp Key",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 2,
                        "name": "PIN",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 3,
                        "name": "Call",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 6,
                        "name": "Speed Dial",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 7,
                        "name": "NULL",
                        "check": True,
                        "selected": False
                    }
                ],
                "homepageLayoutTypeOptions6": [
                    {
                        "id": 1,
                        "name": "Temp Key",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 2,
                        "name": "PIN",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 3,
                        "name": "Call",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 6,
                        "name": "Speed Dial",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 7,
                        "name": "NULL",
                        "check": True,
                        "selected": False
                    }
                ],
                "typeName1": "Call",
                "typeName2": "Temp Key",
                "typeName3": "PIN",
                "isEdit": False,
                "iconShow": True,
                "repeatWeekUtcStr": "1",
                "daySecondsUtcTime": "2025-04-04 08:20:16",
                "treeData": [
                    {
                        "label": "Daily",
                        "value": 0,
                        "children": [
                            {
                                "label": "Monday",
                                "value": 1
                            },
                            {
                                "label": "Tuesday",
                                "value": 2
                            },
                            {
                                "label": "Wednesday",
                                "value": 3
                            },
                            {
                                "label": "Thursday",
                                "value": 4
                            },
                            {
                                "label": "Friday",
                                "value": 5
                            },
                            {
                                "label": "Saturday",
                                "value": 6
                            },
                            {
                                "label": "Sunday",
                                "value": 7
                            }
                        ]
                    }
                ]
            }
        ]
        # ==============================================================================================
        data = {
            "deviceId": "0C110524C835",
            "parentId": "20F83B00E8B2",
            "categoryId": "1818759398759165952",
            "providerId": "1828366320874348544",
            "modelId": "1861386152952885248",
            "accessLcdDto": {
                "id": "1907900349801099264",
                "deviceId": "0C110524C835",
                "enable": True,
                "screensaverTime": 60,
                "screensaverSleepTime": 1800,
                "wakeupMode": 2,
                "themeMode": 0,
                "qrCodeRecognitionInterval": 8,
                "functionOfCallButton": 1,
                "titleOfCallPage": "Call636",
                "titleOfTenantsPage": "Tenant",
                "syncHomePageLayout": True,
                "wakeUpModelOptions": [
                    {
                        "id": 2,
                        "name": "Auto",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 1,
                        "name": "Manual",
                        "check": True,
                        "selected": False
                    }
                ],
                "screensaverSleepTimeOptions": [
                    {
                        "id": 5,
                        "name": "5 seconds",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 10,
                        "name": "10 seconds",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 15,
                        "name": "15 seconds",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 30,
                        "name": "30 seconds",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 60,
                        "name": "1 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 120,
                        "name": "2 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 300,
                        "name": "5 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 600,
                        "name": "10 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 900,
                        "name": "15 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 1800,
                        "name": "30 minutes",
                        "check": True,
                        "selected": True
                    }
                ],
                "screensaverTimeOptions": [
                    {
                        "id": 5,
                        "name": "5 seconds",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 10,
                        "name": "10 seconds",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 15,
                        "name": "15 seconds",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 30,
                        "name": "30 seconds",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 60,
                        "name": "1 minutes",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 120,
                        "name": "2 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 300,
                        "name": "5 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 600,
                        "name": "10 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 900,
                        "name": "15 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 1800,
                        "name": "30 minutes",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 3600,
                        "name": "1 hour",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 7200,
                        "name": "2 hours",
                        "check": True,
                        "selected": False
                    }
                ],
                "functionForCallButtonOptions": [
                    {
                        "id": 0,
                        "name": "Both,Call Default",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 1,
                        "name": "Both,Tenants Default",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 2,
                        "name": "Only Call",
                        "check": True,
                        "selected": False
                    },
                    {
                        "id": 3,
                        "name": "Only Tenants",
                        "check": True,
                        "selected": False
                    }
                ],
                "homepageLayoutTypeOptionsDefault": [
                    {
                        "id": 1,
                        "name": "Temp Key",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 2,
                        "name": "PIN",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 3,
                        "name": "Call",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 6,
                        "name": "Speed Dial",
                        "check": True,
                        "selected": True
                    },
                    {
                        "id": 7,
                        "name": "NULL",
                        "check": True,
                        "selected": True
                    }
                ],
                "homePageLayoutScheduleEntities": homePageLayoutScheduleEntities
            }
        }

        try:
            url = r"https://cloud.connectiot.ai/connect-iot/access/door/device/manage/lcd/setting"
            header = {
                "Content-Type": "application/json",
                "Accept": "application/json, text/plain, */*",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
                "x-access-token": "f7ba9ee502ada8708a4e7d0425bbe72a",
                "cookie": "access_token=f7ba9ee502ada8708a4e7d0425bbe72a; USER_Id=54f1e4c713f00aa5e3ce319e79525ac2; header_building_value=1754083619283746816; header_building_label=Sandbox%20Properties%20600%20Apartment; header_left_menu=[{%22id%22:%22996d740634978615f70ac45b3c1a108d%22%2C%22value%22:%22water%22%2C%22cardTitle%22:%22Smart%20H2O%22%2C%22menuTitle%22:%22Smart%20H2O%22%2C%22selectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809076895994712064.png%22%2C%22noSelectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809077802459303936.png%22%2C%22createTime%22:%221702352059287%22%2C%22sort%22:1}%2C{%22id%22:%223458e0a216161a2489e530fa61b21e76%22%2C%22value%22:%22electricity%22%2C%22cardTitle%22:%22Smart%20Watt%22%2C%22menuTitle%22:%22Smart%20Watt%22%2C%22selectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809076933374349312.png%22%2C%22noSelectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809077826824015872.png%22%2C%22createTime%22:%221702277060612%22%2C%22sort%22:2}%2C{%22id%22:%22f4db87769ca312f5bf7622a0f7c6ca7a%22%2C%22value%22:%22hvac%22%2C%22cardTitle%22:%22Temp%20Sync%22%2C%22menuTitle%22:%22Temp%20Sync%22%2C%22selectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809076959718772736.png%22%2C%22noSelectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809077849984962560.png%22%2C%22createTime%22:%221702277053827%22%2C%22sort%22:3}%2C{%22id%22:%221804131255011422208%22%2C%22value%22:%22access%22%2C%22cardTitle%22:%22Access%22%2C%22menuTitle%22:%22Access%22%2C%22selectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809076998126014464.png%22%2C%22noSelectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809077903277789184.png%22%2C%22createTime%22:%221718973385008%22%2C%22sort%22:4}%2C{%22id%22:%221804131608964542464%22%2C%22value%22:%22sentry%22%2C%22cardTitle%22:%22Sentry%20Eye%22%2C%22menuTitle%22:%22Sentry%20Eye%22%2C%22selectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809077043185422336.png%22%2C%22noSelectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809077925763452928.png%22%2C%22createTime%22:%221718973469380%22%2C%22sort%22:5}%2C{%22id%22:%221765643021285367809%22%2C%22value%22:%22property%22%2C%22cardTitle%22:%22Property%20View%22%2C%22menuTitle%22:%22Property%20View%22%2C%22selectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809077096855736320.png%22%2C%22noSelectedIcon%22:%22https://cloud.connectiot.ai/connect-iot/upload/20240705/1809077112236249088.png%22%2C%22createTime%22:%221709797075013%22%2C%22sort%22:127}]; header_service=1804131255011422208; project_active_value=access"

            }
            ssl_context = ssl.create_default_context()
            ssl_context.check_hostname = False
            ssl_context.verify_mode = ssl.CERT_NONE

            async with aiohttp.ClientSession(
                    connector=aiohttp.TCPConnector(ssl=ssl_context),
                    headers=header
            ) as session:
                async with session.post(url=url, json=data, timeout=10) as response:
                    response.raise_for_status()
                    result = json.loads(await response.text())
                    if result['message'] == "success":
                        print("e16下发成功")
                        print(result)
                    else:
                        print('e16下发失败')
                        print(result)
        except Exception as e:
            print(f"请求失败: {e}")


async def main():
    Temp_Key = 1
    PIN = 2
    Call = 3
    Speed_Dial = 6
    NULL = 7
    param_entity = {
        'type1': Speed_Dial,
        'type2': Call,
        'type3': NULL,
        'displayName1': '111',
        'displayName2': 'pacht2222est1ong2',
        'displayName3': '4444',
    }
    fetcher = SyncToE16Device()
    image_url = await fetcher.sync_to_device(param_entity)


if __name__ == "__main__":
    asyncio.run(main())
